/**
 * ===============================================================================
 * Copyright (C) 2012 Alexandr Tsvetkov
 * Project: TAO Library
 * Package: ua.at.tsvetkov
 * File:    TextViewChecker.java
 * ===============================================================================
 */
package ua.at.tsvetkov.views;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import ua.at.tsvetkov.ui.Rout;
import android.app.Activity;
import android.widget.EditText;

/**
 * EditText view extend by check content methods.
 *
 * @author Alexandr Tsvetkov (2012), http://tsvetkov.at.ua, e-mail: al@ukr.net
 */
public class EditTextChecker {

	private Activity	activity;
	private EditText	editText;

	public EditTextChecker(Activity context, int id) {
		activity = context;
		editText = (EditText) activity.findViewById(id);
	}

	public EditTextChecker(Activity context, EditText view) {
		activity = context;
		editText = view;
	}

	public EditText getView() {
		return editText;
	}

	/**
	 * Set text for field
	 * @param text
	 */
	public void setText(CharSequence text) {
		editText.setText(text);
	}

	/**
	 * Check field and return {@code true} if field is empty and show AlertDialog
	 *
	 * @return
	 */
	public boolean isEmpty(int titleId, int messageId) {
		if (editText.length() == 0) {
			editText.requestFocus();
			Rout.showAlert(activity, titleId, messageId);
			return true;
		}
		return false;
	}

	/**
	 * Check E-mail field and return {@code true} if field is empty and show AlertDialog
	 *
	 * @return
	 */
	public boolean isCorrectEmail(int titleId, int messageId) {
		Pattern p = Pattern.compile(".+@.+\\.[a-z]+");
		Matcher m = p.matcher(editText.getText().toString());
		if (!m.matches()) {
			Rout.showAlert(activity, titleId, messageId);
			return false;
		}
		return true;
	}

	/**
	 * Check password field for match second password field and return {@code true} if field is empty and show AlertDialog
	 *
	 * @return
	 */
	public boolean isPasswordsMatch(EditText secondPasswordField, int titleId, int messageId) {
		if (!editText.getText().toString().equals(secondPasswordField.getText().toString())) {
			Rout.showAlert(activity, titleId, messageId);
			return false;
		}
		return true;
	}

	/**
	 * Check integer value from TextView or EditView
	 *
	 * @param tv - view
	 * @param min
	 * @param max
	 * @return
	 */
	public boolean isIntegerValueCorrect(int min, int max, int titleId, int messageId) {
		int value = getInt(titleId, messageId);
		if ( value < min || value >max) {
			editText.requestFocus();
			Rout.showAlert(activity, titleId, messageId);
			return false;
		}
		return true;
	}

	/**
	 * Check length of content TextView or EditView.
	 *
	 * @param tv - view
	 * @param min
	 * @param max
	 * @return
	 */
	public boolean isCorrectLength(int min, int max, int titleId, int messageId) {
		int value = editText.getText().toString().length();
		if ( value < min || value >max) {
			editText.requestFocus();
			Rout.showAlert(activity, titleId, messageId);
			return false;
		}
		return true;
	}

	/**
	 * Return Integer value of field. If field is not correct AlertDialog is open.
	 *
	 * @return
	 */
	public int getInt(int titleId, int messageId) {
		try {
			return getInt();
		} catch (Exception e) {
			Rout.showAlert(activity, titleId, messageId);
			return 0;
		}
	}

	/**
	 * Return Integer value of field.
	 *
	 * @return
	 */
	public int getInt() {
		if (editText.length() > 0) {
			return Integer.valueOf(editText.getText().toString());
		} else {
			return 0;
		}
	}

	public String getString() {
		return editText.getText().toString();
	}

}
